package com.cs5103.project.calendar;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

@SuppressLint({ "SimpleDateFormat", "CutPasteId" })
public class EventDialog extends DialogFragment {
	private static int titleResourceID;
	private static SharedPreferences mPreferences;
	private static List<CategoryItem> categoryList;
	private String categoryFile;
	
	private final static int END_DATE = 0;
	private final static int START_DATE = 1;
	private final static int DATE_DEFAULT = -1;
	private static int mDateCallerID = DATE_DEFAULT;
	
	private static TextView startDateText;
	private static TextView startTimeText;
	private static TextView endDateText;
	private static TextView endTimeText;
	
	private static SimpleDateFormat dateFormat;
	private static SimpleDateFormat timeFormat;
	
	static EventDialog newInstance(int titleResourceString) {
		EventDialog dlg = new EventDialog();
		titleResourceID = titleResourceString;
		categoryList = new ArrayList<CategoryItem>();
		dateFormat = new SimpleDateFormat("d MMM yy");
		timeFormat = new SimpleDateFormat("h:mm a");

		return dlg;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
		mPreferences.edit();		
		categoryFile = getActivity().getResources().getString(R.string.categoryFileName);
		ReadCategoryList();
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.event_dialog_layout, null);
		builder.setView(v);
		
		@SuppressWarnings("unused")
		EditText eventName = (EditText)v.findViewById(R.id.eventName);
		
		Calendar startDate = (Calendar) CalendarData.get(getActivity()).getDateSelected().clone();

		startDateText = (TextView)v.findViewById(R.id.startDate);
		startDateText.setText(dateFormat.format(startDate.getTime()));
		startTimeText = (TextView)v.findViewById(R.id.startTime);
		startTimeText.setText(timeFormat.format(startDate.getTime()));
		
		Button eventStart = (Button)v.findViewById(R.id.setStart);
		eventStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View startView) {
				// TODO Auto-generated method stub
				PickerDialog mStart = PickerDialog.newInstance(R.string.eventTimeBegin,
						Calendar.getInstance());
				mDateCallerID = START_DATE;
				mStart.show(getFragmentManager(), "pickerDialog");
			}
		});
		
		endDateText = (TextView)v.findViewById(R.id.endDate);
		endDateText.setText(dateFormat.format(startDate.getTime()));
		endTimeText = (TextView)v.findViewById(R.id.endTime);
		endTimeText.setText(timeFormat.format(startDate.getTime()));
		
		Button eventEnd = (Button)v.findViewById(R.id.setEnd);
		eventEnd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View endView) {
				PickerDialog mEnd = PickerDialog.newInstance(R.string.eventTimeEnd,
						Calendar.getInstance());
				mDateCallerID = END_DATE;
				mEnd.show(getFragmentManager(), "pickerDialog");
				
			}
		});
		
		@SuppressWarnings("unused")
		TextView eventEndDate = (TextView)v.findViewById(R.id.endDate);
		
		Spinner eventCategory = (Spinner)v.findViewById(R.id.eventCategorySpinner);
		eventCategory.setAdapter(new CategorySpinnerAdapter(getActivity(),
				R.layout.color_spinner_layout,
				categoryList));

		builder.setTitle(titleResourceID)
			.setIcon(R.drawable.ic_menu_add)
			.setTitle(titleResourceID)
			.setPositiveButton(R.string.buttonOK, 
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				})
			.setNegativeButton(R.string.buttonCancel,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		
		return builder.create();
	}
	
	public static void updateDates(Calendar mNewTime) {
		switch(mDateCallerID) {
			case START_DATE: {
				startDateText.setText(dateFormat.format(mNewTime.getTime()));
				startTimeText.setText(timeFormat.format(mNewTime.getTime()));
				break;
			}
			case END_DATE: {
				endDateText.setText(dateFormat.format(mNewTime.getTime()));
				endTimeText.setText(timeFormat.format(mNewTime.getTime()));
				break;
			}
		}
		mDateCallerID = DATE_DEFAULT;
	}

    private void ReadCategoryList() {
    	ObjectInput in = null;
    	CategoryItem categoryItem = null;
    	CategoryItem categoryTemp = null;
    	
    	try {
    		in = new ObjectInputStream(new FileInputStream(new File(new File(getActivity().getFilesDir(), "")
    							+ File.separator + categoryFile)));
    		while ((categoryTemp = (CategoryItem) in.readObject()) != null) {
    			categoryItem = new CategoryItem(categoryTemp.getCategoryColorID(),
    					categoryTemp.getCategoryName(),
    					categoryTemp.getCategoryColorIconID());
    			categoryList.add(categoryItem);
    		}
    		in.close();
    	}
    	catch (StreamCorruptedException e) {
    		e.printStackTrace();
    	}
    	catch (FileNotFoundException e) {
    		e.printStackTrace();
    	}
    	catch (IOException e) {
    		e.printStackTrace();
    	}
    	catch (ClassNotFoundException e) {
    		e.printStackTrace();
    	}
    }	
}
